// pages/auth/tologin/tologin.js
import Dialog from '../../../assets/framework/vant/dialog/dialog';
var app = getApp();

Page({
  getUserInfo: function () {
    wx.getUserInfo({
      success: function (res) {
        let userInfo = res.userInfo
        app.globalData.userInfo = res.userInfo;
        app.globalData.userInfo.username = "微信用户";
        wx.setStorageSync('userInfo', res.userInfo);
      }
    })
  },
  wxlogin: function (e) {
    let that = this;
    // 判断是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 获取用户信息
          that.getUserInfo();
          // 记录登录状态
          wx.setStorageSync('isLogin', true);
          wx.login({
            success: function (res) {
              var code = res.code; //返回code
              console.log(code);
              // todo /wechat/login
              wx.request({
                  url:"http://localhost:8080/wechat/login",
                  method:"post",
                  data:{
                    jsCode:code,
                    nickName:app.globalData.userInfo.nickName,
                    gender: app.globalData.userInfo.gender,
                    language:'zh',
                    city:'Suzhou',
                    province:'Jiangsu',
                    country:'China'
                  },
                  success:function(res) {
                    wx.setStorageSync('token', "Bearer "+res.data.token)
                    wx.setStorageSync('openId', res.data.openId)
                    wx.switchTab({
                      url: '/pages/home/index/index'
                    })
                  }
              })
            }
          })
        }
      }
    })
  },
  mobilelogin: function (e) {
    let that = this;
    // 判断是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 获取用户信息
          that.getUserInfo();
          // 跳转至手机登录页
          wx.navigateTo({
            url: '/pages/auth/mobile-login/mobile-login',
          })
        }
      }
    })
  },
  pwdlogin: function (e) {
    let that = this;
    // 判断是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 获取用户信息
          that.getUserInfo();
          // 跳转至手机登录页
          wx.navigateTo({
            url: '/pages/auth/pass-login/pass-login',
          })
        }
      }
    })
  }
})